package be.kuleuven.cs.mop.gui.components;


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JScrollPane;



/**
 * Light-weight {@link Container} implementation used for
 *  runtime component switching
 */
public class Wrapper<T extends Component> extends Container {
	private static final long serialVersionUID = 1L;
	
	private T			component;
	private JScrollPane	scroll;
	
	
	public Wrapper() {
		this(new Dimension(400, 300));
	}
	public Wrapper(final Dimension size) {
		setLayout(new BorderLayout());
		setPreferredSize(size);
	}
	public Wrapper(final T component) {
		this();
		set(component);
	}
	
	
	
	public T get() { return component; }
	
	public void set(final T component) {
		this.component = component;
		
		removeAll();
		if (component == null)
			scroll = null;
		else
			add((scroll = toJScrollPane(component)), BorderLayout.CENTER);
	}
	
	@Override
	public void setPreferredSize(final Dimension size) {
		super.setPreferredSize(size);
		if (scroll != null)
			scroll.setPreferredSize(size);
	}
	
	private final JScrollPane toJScrollPane(final Component component) {
		final JScrollPane scroll = new JScrollPane(component,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scroll.setPreferredSize(getPreferredSize());
		return scroll;
	}
	
}